package com.woniuxy.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.TbBedDao;
import com.woniuxy.daos.TbOrderDao;
import com.woniuxy.entitys.TbBed;
import com.woniuxy.entitys.TbOrder;
import com.woniuxy.entitys.TbOrderExample;
import com.woniuxy.producer.OrderProducer;
import com.woniuxy.services.OrderService;
import com.woniuxy.utils.JWTUtils;
import com.woniuxy.utils.LoginCustomer;
import com.woniuxy.utils.OrderCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author wu
 * @date 2024/01/09 14:45:03
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private TbOrderDao tbOrderDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private TbBedDao tbBedDao;
    @Autowired
    private OrderProducer orderProducer;


    @Override
    public Integer guihuan(Integer id,Integer cid) {
        TbOrder order = tbOrderDao.selectByPrimaryKey(id);
        Date date = new Date();
        Date orderTime = order.getOrderTime();
        long l = date.getTime() - orderTime.getTime();
        int l1 = (int) (l / 1000 / 3600);
        if (l/1000%3600>0){
            l1 +=1;
        }
        order.setReturnTime(date);
        order.setOrderstatus(1);
        TbBed tbBed = tbBedDao.selectByPrimaryKey(order.getBid());

        order.setActualpay(BigDecimal.valueOf(l1 * Integer.parseInt(tbBed.getBeiyong1())));
        tbOrderDao.updateByPrimaryKeySelective(order);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("oid", id);
        map.put("cid", cid);
        orderProducer.send(map);
        return 1;

    }

    @Override
    public void updateByonum(String onum) {
        TbOrderExample tbOrderExample = new TbOrderExample();
        TbOrderExample.Criteria criteria = tbOrderExample.createCriteria();
        criteria.andOnumEqualTo(onum);
        TbOrder order = new TbOrder();
        order.setOrderstatus(2);
        order.setPayTime(new Date());
        int i = tbOrderDao.updateByExampleSelective(order, tbOrderExample);
        List<TbOrder> tbOrders = tbOrderDao.selectByExample(tbOrderExample);
        Integer bid = tbOrders.get(0).getBid();
        TbBed tbBed = tbBedDao.selectByPrimaryKey(bid);
        tbBed.setStatus(0);
        int i1 = tbBedDao.updateByPrimaryKeySelective(tbBed);

    }

    @Override
    //下单
    public int insertOrder(TbOrder order) {
        String orderCode = OrderCodeUtils.createOrderCode(new Date());
        order.setOnum(orderCode);
        order.setOrderTime(new Date());
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String token = request.getHeader("token");
        String username = jwtUtils.getUsername(token);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginCustomer customer = (LoginCustomer) valueOperations.get("customer:" + username);
        Integer cid = customer.getCustomer().getCid();
        order.setCid(cid);
        order.setOrderstatus(0);
        order.setSoftdel(1);
        int insert = tbOrderDao.insert(order);

        //下单完成后,修改床位的状态
        Integer bid = order.getBid();
        TbBed tbBed = tbBedDao.selectByPrimaryKey(bid);
        tbBed.setStatus(1);
        tbBedDao.updateByPrimaryKey(tbBed);
        return insert;
    }

    @Override
    public PageInfo<TbOrder> getOrdersByCid(Integer pageNum, Integer pageSize, Integer cid) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbOrder> list = tbOrderDao.selectOrderListByCid(cid);
        PageInfo<TbOrder> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<TbOrder> selOrderByCid(Integer pageNum, Integer pageSize,Integer cid) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbOrder> orders = tbOrderDao.selOrderByCid(cid);
        PageInfo<TbOrder> pageInfo = new PageInfo<>(orders);
        return pageInfo;
    }
}
